using System;
using System.Data;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Configuration.Provider;

/// <summary>
/// A class containing several utility functions used by the SQL Web Tools application
/// </summary>
public class Utility
{
    /// <summary>
    /// Private constructor to keep from instantiating an instance. This class is a utility class and as such, it's
    /// designed to be used by calling each of the static methods
    /// </summary>
	private Utility()
	{
	}

    /// <summary>
    /// Method used to read a boolean value from the provided name value collection and return the value, if 
    /// present, or return the provided default value.
    /// </summary>
    /// <param name="config">The NameValueCollection to search in</param>
    /// <param name="configurationName">The key name to pull the value from</param>
    /// <param name="defaultValue">The default value to return in case the configurationName isn't present, or is empty</param>
    /// <returns></returns>
    public static bool GetBooleanValue(NameValueCollection config, string configurationName, bool defaultValue)
    {
        bool configValue;
        string stringValue = config[configurationName];
        if (String.IsNullOrEmpty(stringValue))
            return defaultValue;

        if (!bool.TryParse(stringValue, out configValue))
            throw new System.Configuration.Provider.ProviderException("Value must not be null");

        return configValue;
    }

    /// <summary>
    /// Method used to read an integer value from the provided NameValueCollection and return the value, if 
    /// present, or return the provided default value if not.
    /// </summary>
    /// <param name="config">The NameValueCollection to search</param>
    /// <param name="configurationName">The key name to retrieve the value from</param>
    /// <param name="defaultValue">The default value to use if the key isn't present</param>
    /// <param name="zeroAllowed">Flag to control whether zero values are valid values</param>
    /// <param name="maxValueAllowed">Integer specifing the maximum value allowed in the configuation</param>
    /// <returns></returns>
    public static int GetIntValue(NameValueCollection config, string configurationName, int defaultValue, bool zeroAllowed, int maxValueAllowed)
    {
        int configValue;
        string stringValue = config[configurationName];
        if (String.IsNullOrEmpty(stringValue))
            return defaultValue;

        if (!int.TryParse(stringValue, out configValue))
        {
            if (zeroAllowed)
                throw new ProviderException("Value must be non negative integer");
            else
                throw new ProviderException("Value must be positive integer");
        }

        if (zeroAllowed && (configValue < 0))
        {
            throw new ProviderException("Value must be non-negative integer");
        }

        if (!zeroAllowed && (configValue <= 0))
        {
            throw new ProviderException("Value must be a positive integer");
        }

        if ((maxValueAllowed > 0) && (configValue > maxValueAllowed))
        {
            throw new ProviderException("Value too big");
        }

        return configValue;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="config"></param>
    /// <param name="configurationName"></param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public static string GetStringValue(NameValueCollection config, string configurationName, string defaultValue)
    {
        string stringValue = config[configurationName];
        if (String.IsNullOrEmpty(stringValue))
            return defaultValue.Trim();
        else
            return stringValue.Trim();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="config"></param>
    /// <param name="configurationName"></param>
    /// <param name="enumerationType"></param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public static object GetEnumValue(NameValueCollection config, string configurationName, Type enumerationType, object defaultValue)
    {
        object configValue;
        string stringValue = config[configurationName];
        try
        {
            configValue = Enum.Parse(enumerationType, stringValue);
            return configValue;
        }
        catch
        {
            return defaultValue;
        }
    }

    /// <summary>
    /// Method used to return the application name
    /// </summary>
    /// <returns></returns>
    public static string GetDefaultApplicationName()
    {
        string appName;
        try
        {
            appName = System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath;
            if (String.IsNullOrEmpty(appName))
            {
                appName = System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName;
                int extensionStart = appName.IndexOf('.');
                if (extensionStart > 0)
                    appName = appName.Remove(extensionStart);
            }

            if (String.IsNullOrEmpty(appName))
                appName = "/";
        }
        catch
        {
            appName = "/";
        }
        return appName;
    }
}
